home *** CD-ROM | disk | FTP | other *** search
- Path: columba.udac.uu.se!news
- From: Enrico Savazzi <enrico.savazzi@pal.uu.se>
- Newsgroups: comp.lang.c++
- Subject: Re: Run time dynamic 2-D array?
- Date: Wed, 14 Feb 1996 15:59:24 +0100
- Organization: Uppsala University
- Message-ID: <3121F8CC.132F@pal.uu.se>
- References: <4flcq9$i1k@vixen.cso.uiuc.edu>
- NNTP-Posting-Host: esavazzi.pal.uu.se
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0 (WinNT; I)
-
- WEIMIN YANG wrote:
- >
- > I need to use a 2-D array. I use following code.
- >
- > void tryit(int a, int b) {
- >
- > float c= new float[b][a];
- >
- > ...
- >
- > ....
- >
- > }
- >
- > I get compile error. Is there another way to do it? By the way, I don't want to
- > use 1-D array to implement it.
-
- Do this:
-
- float** c;
- c = new float* [b];
- for (int i = 0; i < b; i++)
- c[i] = new float [a];
- ....
- for (int i = 0; i < b; i++)
- delete [] c[i];
- delete c;
-
- It is surprising how often this question comes up, and how
- little many C++ books say about it. When I had to tackle this
- problem for the first time, I had to use a uni-dimensional
- array, and found the solution by chance only a few months later.
- Uni-dimensional arrays are faster and contiguous, however, so
- you might still consider using one.
-